package org.come.Jpanel;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.QianDaoBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;

import com.updateNew.MyIsif;
import jdk.internal.dynalink.ChainedCallSite;

import org.apache.commons.collections.CollectionUtils;
import org.come.bean.QianDao;
import org.come.entity.Goodstable;
import org.come.mouslisten.QiandaoGoodsMouselistener;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.MyDayNLJQ;
import org.come.until.UserMessUntil;

import java.awt.*;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.swing.*;

public class QiandaoListJpanel extends JPanel {
    private JLabel labtext1, labtext2, labtext3,// 当前排名
            labtext4,labtext5,labtext6;// 当前排行榜排名
    private JLabel[] fullDayGoods=new JLabel[3];
    private JLabel[] labelList = new JLabel[25];
    private QianDaoBtn qianDaobtn;
    private Integer maxDay = getCurrentMonthDay();
    private int day = 0;
    private Set<Integer> days=new HashSet<>();
    private Set<Integer> lqs=new HashSet<>();
    private int[] s = new int[]{70, 165, 257, 350, 443, 536};
    private int[] sy = new int[]{1, 3, 7, 12, 18, 25};

    public QiandaoListJpanel() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            this.setPreferredSize(new Dimension(800, 600));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 1100);
            offBtn.setBounds(642 - 37, 140, 25, 25);
            this.add(offBtn);
            Font font = new Font("楷体", 0, 18);


            font = new Font("宋体", 0, 20);
            labtext1 = new JLabel();
            labtext1.setForeground(Color.RED);
            labtext1.setFont(font);
            labtext1.setBounds(473, 175, 20, 20);
            labtext1.setText(days.size() + "");
            this.add(labtext1);
            qianDaobtn = new QianDaoBtn("inkImg/old/background/签到按钮.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "签到", this);
            qianDaobtn.setBounds(530, 170, 74, 24);
            this.add(qianDaobtn);

            int x = 0;
            int y = 0;

            for (int i = 1; i <= maxDay; i++) {

                JLabel temp = new JLabel();
                x++;

                if (i % 8 == 0) {
                    x = 0;
                    y++;
                }

                temp.setForeground(Color.BLACK);
                temp.setFont(font);
                temp.setBounds(380 + x * 30, 226 + y * 30, 20, 20);
                temp.setText(i + "");
                this.add(temp);
            }
            Font gf = new Font("宋体", 10, 20);
            //放置物品
            int left = 70;

            List<QianDao> qiandaoBean = UserMessUntil.getQiandaoBean();
            Map<Integer, List<QianDao>> collect = qiandaoBean.stream().collect(Collectors.groupingBy(QianDao::getDay));
            if (collect != null) {
                for (int i = 0; i < 6; i++) {
                    y = -1;
                    JLabel temp;
                    left = s[i];
                    List<QianDao> qianDaos = collect.get(sy[i]);
                    if (qianDaos == null) {
                        continue;
                    }
                    QianDao qianDao = qianDaos.get(0);
                    String goods = qianDao.getGoods();
                    String[] split = goods.split("=")[1].split("&");
                    for (int j = 0; j < 5; j++) {
                        if (j % 2 == 0) {
                            x = 0;
                            y++;
                        }
                        if (j < split.length) {
                            String[] s1 = split[j].split("\\$");
                            Goodstable getgoodstable = UserMessUntil.getgoodstable(BigDecimal.valueOf(Long.parseLong(s1[0])));
                            if (getgoodstable!=null){
                                temp = new JLabel();
                                temp.setForeground(Color.white);
                                temp.setBackground(Color.BLACK);
                                temp.setFont(gf);
                                temp.setBounds(left + x * 40, 411 + y * 40, 40, 40);
                                temp.setIcon(GoodsListFromServerUntil.imgpathAdaptive(getgoodstable.getSkin(), 33, 33));
                                temp.addMouseListener(new QiandaoGoodsMouselistener(getgoodstable));
                                this.add(temp);
                            }
                        }

                        x++;
                    }
                }
                //领取
                QianDaoBtn temp;
                for (int i = 0; i < 6; i++) {
                    temp = new QianDaoBtn("inkImg/old/background/签到按钮.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "领取", sy[i], this);
                    temp.setBounds(70 + i * (97 - (i - 2))+1, 540, 74, 24);
                    this.add(temp);
                }
            }
        } else {
            this.setPreferredSize(new Dimension(800, 600));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/s74.png", 1, 1100);
            offBtn.setBounds(635, 0, 25, 25);
            this.add(offBtn);
            Font font = new Font("楷体", 0, 18);

            labtext3 = new JLabel();
            labtext3.setForeground(Color.YELLOW);
            labtext3.setFont(font);
            labtext3.setBounds(383, 25, 250, 50);
//        labtext3.setText("累计在线时长: "+ RoleData.getRoleData().getLoginResult().getOnlineTime() +" 分");
            labtext3.setText("累计签到:");
            this.add(labtext3);

            labtext4 = new JLabel();
            labtext4.setForeground(Color.YELLOW);
            labtext4.setFont(font);
            labtext4.setBounds(487, 25, 250, 50);
//        labtext3.setText("累计在线时长: "+ RoleData.getRoleData().getLoginResult().getOnlineTime() +" 分");
            labtext4.setText("天");
            this.add(labtext4);

            String[] nljq = MyDayNLJQ.getMyDaynljq();
            String pu = "";
            if (nljq != null && nljq.length > 0) {
                for (String s : nljq) {
                    pu += s + " ";
                }
            }
            font = new Font("宋体", 0, 14);
            labtext5 = new JLabel();
            labtext5.setForeground(Color.green);
            labtext5.setFont(font);
            if (nljq.length == 3) {
                labtext5.setBounds(387, 73, 250, 50);
            } else if (nljq.length == 4) {
                labtext5.setBounds(373, 73, 250, 50);
            } else if (nljq.length == 5) {
                font = new Font("宋体", 0, 12);
                labtext5.setFont(font);
                labtext5.setBounds(355, 73, 250, 50);
            }
            labtext5.setText(pu);
            this.add(labtext5);

            font = new Font("宋体", 0, 20);
            labtext1 = new JLabel();
            labtext1.setForeground(Color.RED);
            labtext1.setFont(font);
            labtext1.setBounds(471, 50, 20, 20);
            labtext1.setText(days.size() + "");
            this.add(labtext1);
            qianDaobtn = new QianDaoBtn("inkImg/hongmu/6026.png", 1, UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, "签到", this);
            qianDaobtn.setBounds(530, 35, 72, 32);
            this.add(qianDaobtn);

            int x = 0;
            int y = 0;

            for (int i = 1; i <= maxDay; i++) {

                JLabel temp = new JLabel();
                x++;

                if (i % 8 == 0) {
                    x = 0;
                    y++;
                }

                temp.setForeground(Color.YELLOW);
                temp.setFont(font);
                temp.setBounds(365 + x * 30, 120 + y * 30, 20, 20);
                temp.setText(i + "");
                this.add(temp);
            }
            Font gf = new Font("宋体", 10, 20);
            //放置物品
            int left = 70;

            List<QianDao> qiandaoBean = UserMessUntil.getQiandaoBean();
            Map<Integer, List<QianDao>> collect = qiandaoBean.stream().collect(Collectors.groupingBy(QianDao::getDay));
            if (collect != null) {
                for (int i = 0; i < 5; i++) {
                    y = -1;
                    JLabel temp;
                    left = s[i];
                    List<QianDao> qianDaos = collect.get(sy[i]);
                    if (qianDaos == null) {
                        continue;
                    }
                    QianDao qianDao = qianDaos.get(0);
                    String goods = qianDao.getGoods();
                    String[] split = goods.split("=")[1].split("&");
                    for (int j = 0; j < 5; j++) {
                        if (j % 2 == 0) {
                            x = 0;
                            y++;
                        }
                        if (j < split.length) {
                            String[] s1 = split[j].split("\\$");
                            Goodstable getgoodstable = UserMessUntil.getgoodstable(BigDecimal.valueOf(Long.parseLong(s1[0])));
                            if (getgoodstable != null) {
                                temp = new JLabel();
                                temp.setForeground(Color.white);
                                temp.setBackground(Color.BLACK);
                                temp.setFont(gf);
                                temp.setBounds(left - 20 + x * 40, 270 + y * 40, 40, 40);
                                temp.setIcon(GoodsListFromServerUntil.imgpathAdaptive(getgoodstable.getSkin(), 31, 31));
                                temp.addMouseListener(new QiandaoGoodsMouselistener(getgoodstable));
                                this.add(temp);
                            }
                        }
                        x++;
                    }
                }

                //满签
                Map<Integer, List<QianDao>> collect2 = qiandaoBean.stream().collect(Collectors.groupingBy(QianDao::getDay));
                if (collect2 != null) {
                    font = new Font("宋体", 0, 12);
                    for (int i = 0; i < 5; i++) {
                        y = -1;
                        left = s[i];
                        List<QianDao> qianDaos = collect2.get(sy[i]);
                        if (qianDaos == null) {
                            continue;
                        }
                        QianDao qianDao = qianDaos.get(0);
                        String goods = qianDao.getDay().toString();
                        labtext6 = new JLabel();
                        labtext6.setForeground(Color.GREEN);
                        labtext6.setFont(font);
                        labtext6.setBounds(50 + i * (97 - (i - 2)) + 1, 247, 85, 32);
                        labtext6.setText("满签" + goods + "天领取");
                        this.add(labtext6);
                    }


                    //领取
                    QianDaoBtn temp;
                    for (int i = 0; i < 5; i++) {
                        temp = new QianDaoBtn("inkImg/hongmu/6026.png", 1, UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, "领取", sy[i], this);
                        temp.setBounds(55 + i * (97 - (i - 2)) + 1, 395, 72, 32);
                        this.add(temp);
                    }
                    //放置满月签到
                    List<QianDao> qianDaos = collect.get(32);
                    if (CollectionUtils.isNotEmpty(qianDaos)) {
                        QianDao qianDao = qianDaos.get(0);
                        String[] split = qianDao.getGoods().split("=")[1].split("&");
                        for (int i = 0; i < split.length; i++) {
                            String s = split[i].split("\\$")[0];
                            Goodstable getgoodstable = UserMessUntil.getgoodstable(BigDecimal.valueOf(Long.parseLong(s)));
                            if (getgoodstable != null) {
                                JLabel jl = new JLabel();
                                jl.setForeground(Color.BLACK);
                                jl.setBackground(Color.BLACK);
                                jl.setFont(gf);
                                jl.setBounds(100 + i * 80, 180, 60, 60);
                                jl.setIcon(GoodsListFromServerUntil.imgpathAdaptive(getgoodstable.getSkin(), 50, 50));
                                jl.addMouseListener(new QiandaoGoodsMouselistener(getgoodstable));
                                this.add(jl);
                            }
                        }
                    }
                }
            }
        }
    }
        private ImageIcon icon2 = CutButtonImage.getImage("inkImg/old/background/S1307.png", 662, 605);
    private ImageIcon icon1 = new ImageIcon("inkImg/old/background/S2941.png");
    private ImageIcon qiandaoed1 = new ImageIcon("inkImg/hongmu/2/打勾.png");
    //红木
    private ImageIcon icon = CutButtonImage.getImage("img/xy2uiimg/qd.png", 640, 400);
    private ImageIcon qiandaoed2 = CutButtonImage.getImage("img/xy2uiimg/21_png.xy2uiimg.atips.png", 20, 20);
    private ImageIcon qiandaoed = new ImageIcon("img/xy2uiimg/打勾.png");

    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();

        a.set(Calendar.DATE, 1);

        a.roll(Calendar.DATE, -1);

        int maxDate = a.get(Calendar.DATE);

        return maxDate;

    }

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            g.drawImage(icon2.getImage(), 0, 0, 659, 602, this);
            g.drawImage(icon1.getImage(), 352, 163, 274, 209, this);
            int x = 0;
            int y =0;
            for (int i = 1; i <= maxDay; i++) {
                x++;

                if (i % 8 == 0) {
                    x = 0;
                    y++;
                }
                if (days.contains(i)) {
                    g.drawImage(qiandaoed.getImage(), 380 + x * 30, 226 + y * 30, 20, 20, this);
                }
            }
            this.labtext1.setBounds(473, 175, 20, 20);
            this.labtext1.setText(days.size()+"");
        } else {
            g.drawImage(icon.getImage(), 0, 0, 659, 472, this);
            int x = 0;
            int y = 0;
            for (int i = 1; i <= maxDay; i++) {
                x++;

                if (i % 8 == 0) {
                    x = 0;
                    y++;
                }
                if (days.contains(i)) {
                    g.drawImage(qiandaoed.getImage(), 363 + x * 30, 120 + y * 30, 20, 20, this);
                    g.drawImage(qiandaoed2.getImage(), 363 + x * 30, 120 + y * 30, 25, 20, this);
                }
            }
            this.labtext1.setBounds(471, 40, 20, 20);
            this.labtext1.setText(days.size() + "");
            this.labtext1.update(g);
        }
    }

    public ImageIcon getIcon1() {
        return icon1;
    }

    public void setIcon1(ImageIcon icon1) {
        this.icon1 = icon1;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public Set<Integer> getDays() {
        return days;
    }

    public void setDays(Set<Integer> days) {
        this.days = days;
    }

    public Set<Integer> getLqs() {
        return lqs;
    }

    public void setLqs(Set<Integer> lqs) {
        this.lqs = lqs;
    }
}
